﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Windows.Forms;
using UCS.Forms.Draughts.ServiceReference;
//using UCS.Forms.Draughts.ServiceReference;

namespace UCS.Forms.Draughts
{
    public partial class FormDraughts : Form
    {
        #region Private Members

        private Draughts _draughts;
        private DraughtsServiceClient _client;
        private UCS.Forms.Draughts.ServiceReference.Player _currentPlayer;

        #endregion

        #region Constructors

        public FormDraughts()
        {
            this._draughts = new Draughts();
            this._client = new DraughtsServiceClient(new InstanceContext(new DraughtsClientCallback(this._draughts)));

            InitializeComponent();
        }

        #endregion

        #region SignedEventMethods

        private void timerUpdate_Tick(object sender, EventArgs e)
        {
            MouseState mouseState = new MouseState();
            mouseState.X = this.PointToClient(Control.MousePosition).X;
            mouseState.Y = this.PointToClient(Control.MousePosition).Y;
            mouseState.LeftButton = (Control.MouseButtons & System.Windows.Forms.MouseButtons.Left) == System.Windows.Forms.MouseButtons.Left ? ButtonState.Pressed : ButtonState.Released;
            mouseState.RightButton = Control.MouseButtons == System.Windows.Forms.MouseButtons.Right ? ButtonState.Pressed : ButtonState.Released;

            this._draughts.Update(mouseState);
            Graphics graphics = this.CreateGraphics();
            DrawingManager.Instance.DrawObjects(graphics);
        }

        private void btnLogIn_Click(object sender, EventArgs e)
        {
            this._currentPlayer = this._client.LogIn();

            if (this._currentPlayer == null)
                MessageBox.Show("O jogo já está cheio!", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
            else
            {
                string text = "Você joga com as peças de cor " + (this._currentPlayer.Color == ServiceReference.PieceColor.Black ? "Preta" : "Branca");
                MessageBox.Show(text, "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        #endregion
    }
}
